//
//  FSCommonTitle.m
//  cocoapodText
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "ShiPei.h"
#import "FSCommonTitle.h"
#import <Foundation/Foundation.h>

@implementation FSCommonTitle



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        //添加子视图
        [self subView];
    }
    return self;
}

//子视图懒加载
-(void)subView{
    
    //TitleLeft
    UILabel *FSTitleViewLeftLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, kStatusH, [ShiPei CGFloatMakeScale:GPDockItemWidth], [ShiPei CGFloatMakeScale:CommonBarH])];
    [FSTitleViewLeftLabel setText:@"贵阳项目"];
    [FSTitleViewLeftLabel setTextColor:[UIColor whiteColor]];
    [FSTitleViewLeftLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:35]]];
    [FSTitleViewLeftLabel setTextAlignment:NSTextAlignmentCenter];
    self.FSTitleViewLeftLabel=FSTitleViewLeftLabel;
    //分割线
    UIImageView *FSTitleLeftDivide1=[[UIImageView alloc]initWithFrame:CGRectMake(0, [ShiPei CGFloatMakeScale:CommonBarH]+kStatusH-2, [ShiPei CGFloatMakeScale:GPDockItemWidth], 1)];
    [FSTitleLeftDivide1 setBackgroundColor:FUIColorFromRGB(0x1467b6)];
    UIImageView *FSTitleLeftDivide2=[[UIImageView alloc]initWithFrame:CGRectMake(0, [ShiPei CGFloatMakeScale:CommonBarH]+kStatusH-1, [ShiPei CGFloatMakeScale:GPDockItemWidth], 1)];
    [FSTitleLeftDivide2 setBackgroundColor:FUIColorFromRGB(0x388bdb)];
    //TitleRight
    UIView *FSTitleViewRight=[[UIView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:GPDockItemWidth],[ShiPei CGFloatMakeScale:kStatusH], kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth], [ShiPei CGFloatMakeScale:CommonBarH])];
    [FSTitleViewRight setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [FSTitleViewRight setUserInteractionEnabled:YES];
    //返回按钮
    UIButton *TitleBackButtonView=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :CommonBarH :CommonBarH]];
    [TitleBackButtonView setImage:[UIImage imageNamed:@"fs_app_back.png"] forState:UIControlStateNormal];
    //[FSTitleViewRight addSubview:TitleBackButtonView];
    self.TitleBackButtonView=TitleBackButtonView;
    //中央标题
    CGFloat titleViewW=400;
    UILabel *TitleCenterLabel=[[UILabel alloc]initWithFrame:CGRectMake((kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth]-titleViewW)/2, 0, titleViewW, [ShiPei CGFloatMakeScale:CommonBarH])];
    [TitleCenterLabel setText:@""];
    [TitleCenterLabel setTextColor:FUIColorFromRGB(0x333333)];
    [TitleCenterLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:35]]];
    [TitleCenterLabel setTextAlignment:NSTextAlignmentCenter];
    [FSTitleViewRight addSubview:TitleCenterLabel];
    self.TitleCenterLabel=TitleCenterLabel;
    //msg
    UIView *TitleMSGView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:680 :0 :CommonBarH :CommonBarH]];
    [TitleMSGView setUserInteractionEnabled:YES];
    UIImageView *TitleMSGImageView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :CommonBarH :CommonBarH]];
    [TitleMSGImageView setImage:[UIImage imageNamed:@"fs_main_message.png"]];
    [TitleMSGView addSubview:TitleMSGImageView];
    
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(messageClick)];
    [TitleMSGView addGestureRecognizer:singleTap];
    
    //[FSTitleViewRight addSubview:TitleMSGView];
    
    self.TitleMSGView=TitleMSGView;
    //warn
    UIView *TitleWARNView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:680+ CommonBarH:0 :CommonBarH :CommonBarH]];
    [TitleWARNView setUserInteractionEnabled:YES];
    UIButton *TitleWARNImageView=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :CommonBarH :CommonBarH]];
    [TitleWARNImageView setBackgroundImage:[UIImage imageNamed:@"fs_main_warn.png"]  forState:UIControlStateNormal];
    [TitleWARNImageView setBackgroundImage:[UIImage imageNamed:@"fs_main_warn_01.png"]  forState:UIControlStateHighlighted];
    [TitleWARNImageView addTarget:self action:@selector(warnInfoClick) forControlEvents:UIControlEventTouchUpInside];
    [TitleWARNView addSubview:TitleWARNImageView];
    [FSTitleViewRight addSubview:TitleWARNView];
    
    self.TitleWARNMarkView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:28 :18 :8 :8]];
    [self.TitleWARNMarkView setImage:[UIImage imageNamed:@"message_state_0.png"]];
    [TitleWARNView addSubview:self.TitleWARNMarkView];
    [self.TitleWARNMarkView setHidden:YES];
    
    UITapGestureRecognizer *singleTap1 =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(warnInfoClick)];
    [TitleWARNView addGestureRecognizer:singleTap1];
    
    self.TitleWARNView=TitleWARNView;
    //userImage
    UIView *TitleUserInfoView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:680+ CommonBarH+CommonBarH+10 :0 :60 :CommonBarH]];
    [TitleUserInfoView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleTap2 =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userImageClick)];
    [TitleUserInfoView addGestureRecognizer:singleTap2];
    
    self.TitleUserInfoView=TitleUserInfoView;
    //头像
    UIImageView *TitleUserInfoImageView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:5 :7.5 :35 :35]];
    [TitleUserInfoImageView.layer setMasksToBounds:YES];
    [TitleUserInfoImageView.layer setCornerRadius:[ShiPei CGFloatMakeScale:17.5]];
    [TitleUserInfoImageView setImage:[UIImage imageNamed:@"user_160*160.jpg"]];
    [TitleUserInfoView addSubview:TitleUserInfoImageView];
    self.TitleUserInfoImageView=TitleUserInfoImageView;
    //下展开
    UIImageView *TitleUserInfoOpenView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:45 :(CommonBarH-5)/2 :10 :5]];
    [TitleUserInfoOpenView setImage:[UIImage imageNamed:@""]];
    [TitleUserInfoView addSubview:TitleUserInfoOpenView];
    [FSTitleViewRight addSubview:TitleUserInfoView];
    self.TitleUserInfoOpenView=TitleUserInfoOpenView;
    //分割线
    UIImageView *FSTitleRightDivide=[[UIImageView alloc]initWithFrame:CGRectMake(0, [ShiPei CGFloatMakeScale:CommonBarH]-2, kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth], 2)];
    [FSTitleViewRight addSubview:FSTitleRightDivide];
    
    GradientView *view2 = [[GradientView alloc] initWithFrame:CGRectMake(0, 0, kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth],2) type:COLOR_GRADIENT_TYPE ];
    view2.alpha = 1.0;
    [FSTitleRightDivide addSubview:view2];
    
    [self addSubview:FSTitleViewLeftLabel];
    [self addSubview:FSTitleLeftDivide1];
    [self addSubview:FSTitleLeftDivide2];
    [self addSubview:FSTitleViewRight];
}


-(void)setTitleText:(NSString *)titleText{
    [self.TitleCenterLabel setText:titleText];
}

//点击忘记密码
-(void)messageClick{
    if([_delegate respondsToSelector:@selector(onMessageClick:)]){
        [_delegate onMessageClick:self];
    }
}

//点击
-(void)warnInfoClick{
    if (_isHaveWarnZhi&&_isHaveWarnDing) {
        //弹出选择预警种类对话框
        UIAlertView *warnSelectAlert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请选择查看预警类型" delegate:self cancelButtonTitle:@"取消查看" otherButtonTitles:@"定额预警",@"指标预警", nil];
        [warnSelectAlert show];
    }else{
        if (!_isHaveWarnZhi&&!_isHaveWarnDing) {
            [CommonTool updateValueWithKey:@"MessageWarnType" value:@""];
            //弹出选择预警种类对话框
            UIAlertView *warnSelectAlert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请选择查看预警类型" delegate:self cancelButtonTitle:@"取消查看" otherButtonTitles:@"定额预警",@"指标预警", nil];
            [warnSelectAlert show];
        }else{
            if(_isHaveWarnDing&&!_isHaveWarnZhi){
                [CommonTool updateValueWithKey:@"MessageWarnType" value:warnDing];
            }
            if (_isHaveWarnZhi&&!_isHaveWarnDing) {
                [CommonTool updateValueWithKey:@"MessageWarnType" value:warnZhi];
            }
            if([_delegate respondsToSelector:@selector(onWarnInfoClick:)]){
                [_delegate onWarnInfoClick:self];
            }
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        //定额预警
        [CommonTool updateValueWithKey:@"MessageWarnType" value:warnDing];
        if([_delegate respondsToSelector:@selector(onWarnInfoClick:)]){
            [_delegate onWarnInfoClick:self];
        }
    }else if(buttonIndex==2){
        //指标预警
        [CommonTool updateValueWithKey:@"MessageWarnType" value:warnZhi];
        if([_delegate respondsToSelector:@selector(onWarnInfoClick:)]){
            [_delegate onWarnInfoClick:self];
        }
    }
}

//切换是否记住密码
-(void)userImageClick{
    if([_delegate respondsToSelector:@selector(onUserImageClick:)]){
        [_delegate onUserImageClick:self];
    }
}

-(void)refreshWarnInfo{
    _warnInfoNum=0;
    _isHaveWarnDing=NO;
    _isHaveWarnZhi=NO;
    APIMessageManager *manager=[APIMessageManager alloc];
    manager.delegate=self;
    [manager fsgy_api_info_message_subscribe_withFalg:MSWarnDing MsuId:@"1" Issub:@"" Year:@"" PageNum:@"" RecoredCount:@"" PageRecord:@""];
    [manager fsgy_api_info_message_subscribe_withFalg:MSWarnZhi MsuId:@"2" Issub:@"" Year:@"" PageNum:@"" RecoredCount:@"" PageRecord:@""];
}

-(void)onMessageManager:(APIMessageManager *)manager result:(NSDictionary *)result Flag:(NSString *)flag{
    [self addWarnInfoMark];
    _warnInfoNum++;
    if ([flag isEqualToString:MSWarnDing]) {
        //解析数据
        NSArray *list=[result objectForKey:@"list"];
        //是否有预警信息
        if ([list count]>0) {
            _isHaveWarnDing=YES;
        }else{
            _isHaveWarnDing=NO;
            if (_warnInfoNum==2) {
                if (!_isHaveWarnZhi) {
                    //没有预警信息
                    [self clearWarnInfoMark];
                }
            }
        }
    }else if([flag isEqualToString:MSWarnZhi]){
        //解析数据
        NSArray *list=[result objectForKey:@"list"];
        //是否有预警信息
        if ([list count]>0) {
            _isHaveWarnZhi=YES;
        }else{
            _isHaveWarnZhi=NO;
            if (_warnInfoNum==2) {
                if (!_isHaveWarnDing) {
                    //没有预警信息
                    [self clearWarnInfoMark];
                }
            }
        }
    }
}

-(void)clearWarnInfoMark{
    [self.TitleWARNMarkView setHidden:YES];
}

-(void)addWarnInfoMark{
    [self.TitleWARNMarkView setHidden:NO];
}


@end
